{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 方根"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 简介"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "方根运算是高等数学非常重要的概念，而且容易和[对数运算](https://math.haozi.me/log.html)、[指数运算](https://math.haozi.me/pow.html)混淆"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 定义"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果一个数满足 $x^n = a$，则 $x$ 叫做 $a$ 的 $n$次方根，求 x 的运算记作 $\\sqrt[n]a$，读作 $a$ 的 $n$ 次方根。\n",
    "\n",
    "常见的，当 $n=2$时，称作平方根，一般省略根号上的 2，简写成$\\sqrt a$，当$n=3$时称作立方根。\n",
    "\n",
    "平方根：如果一个数 $x$ 的平方等于 $a$ 即$x^2 = a$,那么这个数$x$叫做$a$的平方根。如 $2^2=4$, $(-2)^2 = 4$。所以 $\\pm 2$都是 4 的平方根\n",
    "\n",
    "算术平方根：其中大于 0 的那个根，叫做算数平方根，如上，2 是 4 的算术平方根，-2 则不是。特别规定，0 的算术平方根是 0\n",
    "\n",
    "立方根没有规定算术立方根，因为 a 可以是负数。\n",
    "\n",
    "另外，负数的方根有虚数解。虚数概念先 TODO。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 运算法则"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "${\\sqrt[ {n}]{ab}}={\\sqrt[ {n}]{a}}{\\sqrt[ {n}]{b}}\\qquad a\\geq 0,b\\geq 0$\n",
    "\n",
    "${\\sqrt[ {n}]{{\\frac  {a}{b}}}}={\\frac  {{\\sqrt[ {n}]{a}}}{{\\sqrt[ {n}]{b}}}}\\qquad a\\geq 0,b>0$\n",
    "\n",
    "${\\sqrt[ {n}]{a^{m}}}=\\left({\\sqrt[ {n}]{a}}\\right)^{m}=\\left(a^{{{\\frac  {1}{n}}}}\\right)^{m}=a^{{{\\frac  {m}{n}}}}$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Python 可以求解复数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\sqrt{2}$"
      ],
      "text/plain": [
       "sqrt(2)"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sympy import *\n",
    "sqrt(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\sqrt{2} i$"
      ],
      "text/plain": [
       "sqrt(2)*I"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sqrt(-2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\sqrt[3]{-1}$"
      ],
      "text/plain": [
       "(-1)**(1/3)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cbrt(-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[-1, 1/2 - sqrt(3)*I/2, 1/2 + sqrt(3)*I/2]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = Symbol('x')\n",
    "solve(x ** 3 + 1, x) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "-1 的立方根可以看做方程 x ** 3 = -1 的解, 我们用 Python 解这个方程，发现除了 -1, 还有另外两个复数解，可以参考本人的[这篇知乎](https://www.zhihu.com/question/274961604/answer/377858864)。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 证明分数次幂与开方运算等价"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "求证 $a^{\\frac{m}{n}} = \\sqrt[n]{{a ^m}} (a>0, m、n \\in \\mathbb{Z} 且n>1)$\n",
    "\n",
    "证：\n",
    "  令 $\\sqrt[n]{{a ^m}} = b$\n",
    "  \n",
    "  两边同乘 n 次方，有\n",
    "  \n",
    "  $a^m = b^n$\n",
    "  \n",
    "  两边同乘 $\\frac{1}{n}$ 次方，有\n",
    "  \n",
    "  $a^{m * (\\frac{1}{n})} = a^{\\frac{m}{n}} = b^{n* \\frac{1}{n}} = b$\n",
    "  \n",
    "  又∵ $\\sqrt[n]{{a ^m}} = b$\n",
    "  \n",
    "  ∴ 命题得证\n",
    "  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "其实上面的限定条件可以进一步扩展到整个复数域。\n",
    "\n",
    "在使用中，数学家更青睐使用分数次幂表示开方运算，加上负数次幂的定义(有兴趣的同学欢迎[留言](https://github.com/easy-math/Math/issues/16)证明，感谢[@songofhawk](https://github.com/songofhawk) 的[证明](https://math.haozi.me/negative_power.html#%E8%B4%9F%E6%AC%A1%E5%B9%82))，这使得指数运算扩展到实数域。后来欧拉又将指数运算扩展到无理数和复数域。从此整个幂运算就和开方运算等价起来了。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## JS 实现"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "由于我的系列还没有讲到导数，甚至连三角函数和斜率都还没有讲。但大部分人在高中有学过一些，这里先不展开，直接给出牛顿公式："
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$x_{n+1}=x_n-\\frac{f(x_n)}{f^{'}(x_n)}=\\frac{(x_n+\\frac{a}{x_n})} {2}$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```javascript\n",
    "// 求 2 次方程近似解：\n",
    "const sqrt = (a) => {\n",
    "  let ret = 1\n",
    "  const Max = 50\n",
    "  for (let i = 0; i < Max; i++) {\n",
    "    ret = (ret + a/ret) / 2\n",
    "  }\n",
    "  return ret\n",
    "}\n",
    "\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们可以看到，迭代次数要随着被开方的数增大而增大。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 应用"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "开方运算在统计降维、几何学等领域有非常重要的应用。关于开方运算的讨论，欢迎在这里[留言](https://github.com/easy-math/Math/issues/16)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
